from drivers.plc_modbus_base import PlcModbusBase

def test_plc_communication():
    # 创建PLC通信实例
    plc = PlcModbusBase(ip="192.168.3.39", port=502)

    # 测试写入单字
    plc.write_word(100, 12345)  # 写入地址100，值为12345

    # 测试写入双字
    plc.write_dword(200, 987654321)  # 写入地址200，值为987654321

    # 测试写入字符串（指定15个字的长度）
    text = "Hello PLC Test"
    plc.write_string(300, text, word_length=15)  # 会自动补齐到30个字符

    # 测试读取单字
    value1 = plc.read_word(100)
    print(f"Read word: {value1}")  # 应该输出12345

    # 测试读取双字
    value2 = plc.read_dword(200)
    print(f"Read dword: {value2}")  # 应该输出987654321

    # 测试读取字符串
    value3 = plc.read_string(300, word_length=15)
    print(f"Read string: {value3}")  # 应该输出"Hello PLC Test"

if __name__ == "__main__":
    test_plc_communication() 